import os
import time
import xlrd


questions = {}
# questions['1.1.1'] = ['软件发展经历三个阶段','程序设计 程序系统 软件工程']
# questions['1.1.2'] = ['软件的概念','软件是计算机系统中与硬件相互依存的另一部分 它包括程序 数据 及其相关文档的完整集合']
# questions['1.1.3.1'] = ['软件危机的概念','软件危机是在计算机软件开发和维护过程中所遇到的一系列严重问题']
# questions['1.1.3.2'] = ['软件危机包含两方面内容','1 如何开发软件 以满足对软件日益增长的需求 2 如何维护数量不断膨胀的已有软件']

def judge(ans,refer):
    if ans == "show":
        print(refer)
        ans = input()
        os.system("cls")
        print("")
        ans = input()
    if ans == "pass":
        return True
    if ans != refer:
        for i in range(len(refer)):
            try:
                if ans[i] == refer[i]:
                    print(ans[i],end="")
                else:
                    if refer[i] == ' ':
                        print("—",end="")
                    else:
                        print("。",end="")
            except BaseException:
                if refer[i] == ' ':
                    print("—",end="")
                else:
                    print("。",end="")
        print()
    return ans == refer

def question(key):
    id = key
    quest = questions[key]["quest"]
    refer = questions[key]["refer"]
    print("")
    ans = ""
    print(id + " " + quest)
    while True:
        ans = input()
        if judge(ans,refer):
            os.system("cls")
            print("congratulation !")
            print(refer)
            break
        else:
            print("failed !")

def show_quest_list(left_id, right_id,is_all):
    for key in questions.keys():
        if left_id <= key <= right_id:
            print(key + " " + questions[key]["quest"])
            if is_all == 'y':
                print(questions[key]["refer"])

def start_game(start_id):
    for key in questions.keys():
        if key >= start_id:
            question(key)
    print("你通关了!")
    time.sleep(10)
def init():
    global questions
    work_book = xlrd.open_workbook(r'data.xls')
    sheet = work_book.sheet_by_index(0)
    length = sheet.nrows
    for i in range(0,length):
        row_values = sheet.row_values(i)
        questions[row_values[0]] = {"quest":row_values[1],"refer":row_values[2]}

def main():
    init()
    # print(questions)
    os.system("color 0a")
    while True:
        print("there are some cmds of this system")
        print("show   show question list.   cmd-params:f(left-id, right-id, is_all)   show 1.1.1 1.1.2 y")
        print("do     start game.           cmd-params:f(start-id)                    do 1.1.2")
        print("quit   exit the system.      cmd-params:none                           quit")        
        cmds = input().split(" ")
        os.system("cls")
        try:
            if cmds[0] == "show":
                print()
                show_quest_list(cmds[1],cmds[2],cmds[3])
                print("\n\npress enter to continue...")
                input()
                os.system("cls")
            elif cmds[0] == "do":
                print()
                start_game(cmds[1])
                os.system("cls")
            elif cmds[0] == "quit":
                print()
                print("Bye")
                break
        except BaseException:
            print("some errors happened.")
            continue



if __name__ == '__main__':
    main()


